package UI;




import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;

import Commnads.GuiCommands;


/**
 * Servlet implementation class SearchServ
 */
	public class SearchServ extends HttpServlet 
	{
		private static final long serialVersionUID = 1L;
	    private GuiCommands command;   
	    public SearchServ() 
	    {
	        super();
	    }
	    
	    public SearchServ(GuiCommands com) 
	    {
	    	command = com;
	    }
	    
		/**
		 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		 */
	    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException 
	    {
	    		String yt = req.getParameter("yes");
	    		String nt = req.getParameter("no");
	    		String[] yesTags = null;
	    		yesTags = yt.split(",");
	    		String[] noTags = null;
	    		noTags = nt.split(",");
	    		
	    		Set<String> yesSet = new HashSet<String>();
	    		for(int i=0; i< yesTags.length; i++)
	    			yesSet.add(yesTags[i]);
	    		
	    		Set<String> noSet = new HashSet<String>();
	    		for(int i=0; i< noTags.length; i++)
	    			noSet.add(noTags[i]);
	    		
	    		ArrayList<String> resualt = null;
	    		String res = "";
	    		resualt = command.searchByTag(yesSet, noSet);
	    		if(resualt != null)
	    		{
	    			Iterator<String> it = resualt.iterator();
	    			while(it.hasNext())
	    			{
	    				String path = it.next();
	    				String words[]=path.split("\\\\");
	    				int len= words.length;
	    				String fileName =  words[len-1];
	    				path = path.replaceAll("\\\\", "\\\\\\\\");
	    				res += "File: "+fileName +"\\n"+"Path: "+path+"\\n\\n";
	    			}
	    		}
	    		String answer = "<script> parent.show_resault.show_Files.value = '"+res+"'; </script>";
	    		resp.getWriter().println(answer);
	    		resp.getWriter().flush();
	    		
    }

}
